package com.internetCafes.spms.web.app.service.impl;


import com.internetCafes.spms.core.exception.RRException;
import com.internetCafes.spms.web.app.dao.UserMapper;
import com.internetCafes.spms.web.app.entity.UserEntity;
import com.internetCafes.spms.web.app.form.LoginForm;
import com.internetCafes.spms.web.app.service.UserService;
import com.internetCafes.spms.web.sys.model.UserInfo;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.HashMap;
import java.util.Map;


@Service("userService")
public class UserServiceImpl implements UserService {

	@Autowired
	private UserMapper userMapper;

	@Override
	public UserEntity queryByMobile(String mobile) {
		Map<String, Object> params = new HashMap<>();
		return userMapper.selectByParams(params).get(0);
	}



	@Override
	public long login(LoginForm form) {
		Map<String, Object> params = new HashMap<>();
		UserEntity user = userMapper.selectByParams(params).get(0);
		Assert.isNull(user, "手机号或密码错误");

		//密码错误
		if(!user.getPassword().equals(DigestUtils.sha256Hex(form.getPassword()))){
			throw new RRException("手机号或密码错误");
		}

		return user.getUserId();
	}

	@Override
	public void save(UserEntity user) {
		userMapper.insert(user);
	}
}
